var app = angular.module('JiangGanApp')
app.service('DiagramService',Diagram)

function Diagram(){
	this.getRadar=function(dataMap){
		var option = {
			grid:{
				containLabel:true
			},
		    tooltip: {
		    	trigger:'item',
		    	position:'right'
		    },
		    radar: {
		    	radius:'55%',
		    	nameGap:5,
		        indicator: dataMap.indicator,
		        axisLine: {
		            show: true,
		            lineStyle:{
		                color:'#8499C1'
		        	}
		        },
	        	splitArea: {
            		areaStyle: {
               			 color: ['#fff']
            		}
        		},
        		name:{
	            	show:true,
	            	textStyle:{
	                	fontSize:14,
	                	color:' #131313'
	            	}
	        	},
	        	splitLine: {
            		lineStyle: {
                		color: '#8499C1'
            		}
       	 		}
		    },
		    series: [{
		        name: dataMap.name,
		        type: 'radar',
		        data : [
		            {
		                value : dataMap.data,
		                name : ''
		            }
		        ],
		        itemStyle:{
		        	normal:{
		        		color:'#3980FF',
		        		borderColor:'#3980FF',
		        		borderWidth:2
		        	}
		        },
		        areaStyle: {
                    normal: {
						color: {
						    type: 'radial',
						    x: 0.5,
						    y: 0.5,
						    r: 0.5,
						    colorStops: [{
						        offset: 0, color: '#C9DCFF' // 0% 处的颜色
						    }, {
						        offset: 1, color: ' #3980FF' // 100% 处的颜色
						    }],
						    globalCoord: false // 缺省为 false
						}
                    }
                }
		    }]
		};
		return option;
	}

	  //描绘环装饼图
  this.drawPie = function (dataMap){
    var series = dataMap.series;
    var length = series.length;
    var colorGroup = ['#F4342F','#F2983C','#317AFF']
    var option = {
	    tooltip: {
	        trigger: 'item',
	        formatter: "{a} <br/>{b}: {c} ({d}%)"
	    },
	    legend: {
	        orient: 'vertical',
	        x: 'left',
	        data:['直接访问','邮件营销','联盟广告','视频广告','搜索引擎']
	    },
	    series: [
	        {
	            name:'访问来源',
	            type:'pie',
	            radius: ['50%', '70%'],
	            avoidLabelOverlap: false,
	            label: {
	                normal: {
	                    show: false,
	                    position: 'center'
	                },
	                emphasis: {
	                    show: true,
	                    textStyle: {
	                        fontSize: '30',
	                        fontWeight: 'bold'
	                    }
	                }
	            },
	            labelLine: {
	                normal: {
	                    show: false
	                }
	            },
	            data:[
	                {value:335, name:'直接访问'},
	                {value:310, name:'邮件营销'},
	                {value:234, name:'联盟广告'},
	                {value:135, name:'视频广告'},
	                {value:1548, name:'搜索引擎'}
	            ]
	        }
	    ]
	};
	return option;
  }

  this.getCommBar = function(type,data,max){
	var dataShadow = [];

	for (var i = 0; i < data.length; i++) {
    	dataShadow.push(max);
	}

	var option = {
		grid:{
			top:'5',
			bottom:'5',
			left:'80',
			right:'50'
		},
	    yAxis: {
	        data: type,
	        axisTick: {
	            show: false
	        },
	        axisLine: {
	            show: true,
	            lineStyle:{
	                color:'#AFC2E4'
	            }
	        },
	        axisLabel:{
	            show:true,
	            textStyle:{
	                fontSize:12,
	                color:' #666666'
	            }
	        },
	        splitLine:{
	            show:false
	        },
	        z: 10
	    },
	    xAxis: {
	        show:false
	    },
	    backgroundColor:{
	      color:'#fff'  
	    },
	    series: [
	        { // For shadow
	            type: 'bar',
	            itemStyle: {
	                normal: {
	                    color: '#DCE7F9',
	                    barBorderRadius:[2,2,2,2]
	                },
	                emphasis:{
	                    color: '#DCE7F9',
	                    barBorderRadius:[2,2,2,2]
	                }
	            },
	            barGap:'-100%',
	            barCategoryGap:'35%',
	            data: dataShadow,
	            animation: false,
	            z:1
	        },
	        {
	            type: 'bar',
	            label:{
	                normal:{
	                    show:true,
	                    position:['150','0'],
	                    formatter:'{c}分',
	                    textStyle:{
	                        color:'#666666',
	                        fontSize:14
	                    }
	                }
	            },
	            itemStyle:{
	                normal:{
	                    barBorderRadius:[2,2,2,2],
	                    color:function(params){
	                        if(params.value>5){
	                            return '#FF817C';
	                        }else if(params.value>3){
	                            return '#FFDD67';
	                        }else{
	                            return '#639BFF';
	                        }
	                    }
	                }
	            },
	            data:data,
	            z:2
	        }
	        
	    ]
	}
	return option;
  }

  this.getLine = function(title,legend,xAxis,data){
	var option = {
	    title: {
	        text: title,
	        top:'10',
	        left:'10'
	    },
	    // dataZoom:[{
	    // 	type:'slider',
	    // 	show:true,
	    // 	start:77,
	    // 	end:100
	    // }
	    // ],
	    tooltip: {
	        trigger: 'axis'
	    },
	    legend: {
	        data:legend,
	        top:'40',
	        align:'right',
	        right:'50'     
	    },
	    backgroundColor:'#F2F7FF',
	    grid: {
	    	show:true,
	        bottom:'40',
	        right:'50',
	        top:'70',
	        borderColor:'#8A8DCF'
	    },
	    xAxis: {
	        type: 'category',
	        axisLine:{
	        	show:false
	        },
	        axisTick:{
	        	show:false
	        },
	        splitLine:{
	        	show:false
	        },
	        boundaryGap: false,
	        data: xAxis
	    },
	    yAxis: {
	        type: 'value',
	        axisLine:{
	        	show:false
	        },
	        splitLine:{
	        	show:true,
	        	lineStyle:{
	        		color:'#8A8DCF'
	        	}
	        },
	        axisTick:{
	        	show:false
	        }
	    },
	    color:['#2098FF','#6B6FBF'],
	    series: [
	    ]
	};


    for(var i = 0 ;i<legend.length;i++){
    	var serie = {
	        name:legend[i],
	        type:'line',
	        smooth:true,
	        symbolSize:14,
	        data:data[i]
	    };
	    option.series.push(serie)
    }

	return option;

  }

  this.getStackBar = function(title,legend,xAxis,data){
	var option = {
	    title: {
	        text: title,
	        top:'10',
	        left:'10'
	    },
	    tooltip: {
	        trigger: 'axis'
	    },
	    // dataZoom:[{
	    // 	type:'slider',
	    // 	show:true,
	    // 	start:77,
	    // 	end:100
	    // }
	    // ],
	    legend: {
	        data:legend,
	        top:'40',
	        align:'right',
	        right:'50'     
	    },
	    backgroundColor:'#F2F7FF',
	    grid: {
	    	show:true,
	        bottom:'20',
	        right:'10%',
	        top:'70',
	        borderColor:'#8A8DCF',
	        containLabel:true
	    },
	    xAxis: {
	        axisLine:{
	        	show:false
	        },
	        axisTick:{
	        	show:false
	        },
	        splitLine:{
	        	show:false
	        },
	        data: xAxis
	    },
	    yAxis: {
	        axisLine:{
	        	show:false
	        },
	        splitLine:{
	        	show:true,
	        	lineStyle:{
	        		color:'#8A8DCF'
	        	}
	        },
	        axisTick:{
	        	show:false
	        }
	    },
	    color:['#6B6FBF','#2098FF'],
	    series: [
	    ]
	};


    for(var i = 0 ;i<legend.length;i++){
    	var serie = {
    		stack:'总量',
	        name:legend[i],
	        type:'bar',
	        barCategoryGap:'50%',
	        data:data[i]
	    };
	    option.series.push(serie)
    }

	return option;

  }
}